package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.itheima.config.RedisUtils;
import com.itheima.constant.RoomHotelConstant;
import com.itheima.entity.AccountNotification;
import com.itheima.entity.SysUser;
import com.itheima.mapper.AccountNotificationMapper;
import com.itheima.pojo.EmailProperties;
import com.itheima.query.SysUserQuery;
import com.itheima.mapper.SysUserMapper;
import com.itheima.service.ISysUserService;
import com.itheima.utils.MailUtil;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;

import static com.itheima.utils.JwtUtil.parseToken;

/**
 * 系统用户(SysUser)表服务实现类
 *
 * @author guanhuaer
 * @since 2024-06-28 12:47:48
 */
@Service
public class SysUserServiceImpl implements ISysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private EmailProperties emailProperties;
    @Autowired
    private AccountNotificationMapper notificationMapper;

    /**
     * 查询所有数据
     *
     * @return 实例对象集合
     */
    @Override
    public List<SysUser> queryAll() {
        return this.sysUserMapper.queryAll(null);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SysUser queryById(Long id) {
        return this.sysUserMapper.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param sysUser 实例对象
     * @return 实例对象
     */
    @Override
    public SysUser insert(SysUser sysUser) {
        this.sysUserMapper.insert(sysUser);
        return sysUser;
    }

    /**
     * 修改数据
     *
     * @param sysUser 实例对象
     * @return 实例对象
     */
    @Override
    public SysUser update(SysUser sysUser) {
        this.sysUserMapper.update(sysUser);
        return this.queryById(sysUser.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.sysUserMapper.deleteById(id) > 0;
    }

    @Override
    public List<SysUser> queryPage(SysUserQuery sysUserQuery) {
        return null;
    }

    /**
     * 发送邮箱验证码
     * @param
     * @return
     */
    public String code(SysUser user) {
        /**
         * @param to 收件人邮箱
         * @param title 邮件标题
         * @param content 邮件正文
         * @return
         */
        String vscode ="";
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            Integer number = random.nextInt(0,9);
            vscode+=number;
        }
        if (user.getEmail()==null){
            redisUtils.set(RoomHotelConstant.USER_PWD_CODE +vscode,vscode,60*5);
            return vscode;
        }
        String title="大学生创新创业管理系统";
        String content = "【验证码】当前你的验证码是："+vscode+"如非本人操作，请忽略本邮件。五分钟之内有效";
        boolean flag = MailUtil.sendMail(emailProperties,user.getEmail(),title,content);
        //将验证码放入redis中并设置5分钟过期
        System.out.println(vscode);
        redisUtils.set(RoomHotelConstant.USER_CODE +user.getEmail(),vscode,60*5);
        return null;
    }

    /**
     * 根据用户名称和邮箱查询是否存在
     * @param username
     * @param email
     * @return
     */
    @Override
    public SysUser selectUsernsmeAndEmail(String username, String email) {
        return sysUserMapper.selectUsernsmeAndEmail(username,email);
    }

    /**
     * 用户注册
     * @param user
     */
    @Override
    public void register(SysUser user) {
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        user.setStatus(0);
        user.setOccupation(0);
        user.setIsAuthentication(0);
        user.setIsReset(0);
        user.setCreateDate(new Date());
        user.setUpdateDate(new Date());
        sysUserMapper.insert(user);
        // 设置用户信息通知
        AccountNotification notification = new AccountNotification();
        notification.setUserId(getUserId());
        notification.setNotice(0L);
        notification.setCreateDate(LocalDateTime.now());
        notification.setSystems(0L);
        notification.setMessage(0L);
        notificationMapper.insertAccountNotification(notification);
    }
    private Long getUserId() {
        try {
            return sysUserMapper.selectMaxId();
        } catch (Exception e) {
            return 1L;
        }
    }

    @Override
    public void reset(SysUser user) {
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        sysUserMapper.updatePassword(user);
    }

}
